/******************************************************************************
 * Author:      671643387
 * Created:     2016/11/19
 *****************************************************************************/

///////////////////////////////////////////////////////////////////////////////
//>> GM模块
var util = require("util");

/**
 * 查询玩家数据
 * @param query
 * @param httpReq
 * @param httpRes
 */
exports.queryUser = function(query, httpReq, httpRes) {
    var uid = +query.uid;

    PlayerMgr.getPlayer(uid, function(player){
        if (player) {
            var user = player.user;
            endReq({
                name    : user.info.name,       // 玩家名字
                card    : user.status.card,     // 玩家房卡数量
            }, httpRes);
        } else {
            endReq({
                ok      : false,
            }, httpRes);
        }
    });
};

/**
 * 查询玩家房卡
 * @param query
 * @param httpReq
 * @param httpRes
 */
exports.queryCard = function(query, httpReq, httpRes) {
    var uid = +query.uid;
    DEBUG("http  modifyCard ======= " + uid);
    PlayerMgr.getPlayer(uid, function(player){
        if (player) {
            endReq({
                card   : player.user.status.card, // 玩家房卡
            }, httpRes);
        } else {
            endReq({
                ok      : false,
            }, httpRes);
        }
    });
};

/**
 * 修改玩家房卡
 * @param query
 * @param httpReq
 * @param httpRes
 */
exports.modifyCard = function(query, httpReq, httpRes) {
    var uid = +query.uid;
    var card = +query.card;
    DEBUG("http  modifyCard =======  " + uid);
    PlayerMgr.getPlayer(uid, function(player){
        if (player) {
            if (player.modifyCard(card)) {
                player.addCardsRecord(card, 1, 'gm');
                endReq({ok      : true,}, httpRes);
            } else {
                endReq({
                    ok      : false,
                }, httpRes);
            }
        }
    });
};


/**
 * 冻结玩家账号
 * @param query
 * @param httpReq
 * @param httpRes
 */
exports.lockPlayer = function(query, httpReq, httpRes) {
    var uid = +query.uid;

    PlayerMgr.lockUser(uid, function (player) {
        if (player) {
            LOG("lockPlayer " + uid + " OK");
            var user = player.user;
            endReq({
                name: user.info.name,       // 玩家名字
                uid: uid,
                ok: true,
            }, httpRes);
        } else {
            MongoUser.findOneAndUpdate({_id: uid}, {"$set": {"lock": 1}}, function (err, result) {
                if (!err) {
                    MongoUser.findOne({_id: uid}, {}, function(err, doc) {
                        if (doc) {
                            LOG(util.format("Uid %s lock", uid));
                            endReq({
                                name: doc.info.name,       // 玩家名字
                                uid: doc._id,
                                ok: true,
                            }, httpRes);
                        } else {
                            LOG("lockPlayer " + uid + " Not player ");
                            endReq({
                                ok: false,
                            }, httpRes);
                        }
                    });
                } else {
                    LOG("lockPlayer " + uid + " Not player ");
                    endReq({
                        ok: false,
                    }, httpRes);
                }
            }.bind(this));
        }
    });
};

/**
 * GM修改指定玩家房卡
 * @param query
 * @param httpReq
 * @param httpRes
 */
exports.gmModifyUserCard = function(query, httpReq, httpRes) {
    var uid = +query.uid;
    var card = +query.card;

    DEBUG("http  gmModifyUserCard =======  " + uid);
    PlayerMgr.findPlayer(uid, function(player){
        if (player) {
            player.addCardsRecord(card, 1, 'gm');
            if (player.modifyCard(card)) {
                endReq({ok  : true}, httpRes);
                return;
            }else{
                endReq({ok  : false}, httpRes);
                return;
            }
        }else{
            MongoUser.findOne({_id: uid}, {}, function(err, doc) {
                if (doc) {
                    LOG(util.format("gmModifyUserCard %s Card", uid));
                    var TotalCard = doc.status.card + card;
                    MongoUser.findOneAndUpdate({_id: uid}, {"$set": {"status.card": TotalCard}}, function (err, result) {
                        if (!err) {
                            LOG("gmModifyUserCard " + uid + " Change Card OK");
                            endReq({
                                ok: true,
                            }, httpRes);
                            return;
                        } else {
                            LOG("gmModifyUserCard " + uid + " Change Card Error");
                            endReq({
                                ok: false,
                            }, httpRes);
                        }
                    }.bind(this));
                } else {
                    LOG("gmModifyUserCard " + uid + " Not player ");
                    endReq({
                        ok: false,
                    }, httpRes);
                }
            });
        }
    });
};


///////////////////////////////////////////////////////////////////////////////
//>> 帮助函数
function endReq(json, httpRes) {
    httpRes.end(JSON.stringify(json));
}